﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.CMS.Plugins;
using MethodWorx.CMS.Content;
using System.Web;
using MethodWorx.CMS.TemplateLanguage;
using MethodWorx.CMS.Providers;

namespace MethodWorx.CMS.Administration
{
    public class HostedApplicationType : ApplicationType, IPlugin
    {
        private static IdeExtension urlRoutingIdeExtension;
        private static PageTmlExtension urlTmlExtension;

        public Guid Id { get { return this.ApplicationTypeId; } }

        static HostedApplicationType()
        {
            urlRoutingIdeExtension = new IdeExtension
            {
                Id = Guid.Parse("{0345D98F-7F26-486E-92D5-D51732606072}"),
                DisplayName = "URL Routing",
                Name = "UrlRouting",
                WebEditor = new AssemblyResourceReference(typeof(HostedApplicationType).Assembly, "Plugins/Views/PageTemplate/HostedApplicationTypeUrlRouting.ascx"),
                Controller = typeof(HostedApplicationTypeUrlRoutingIdeExtension)
            };

            urlTmlExtension = new PageTmlExtension()
            {
                Name = "Url",
                Namespace = "Page",
                Parser = (context, page, parameters) =>
                    {
                        return HostedApplicationType.GetPreviewUrlForPage(page);
                    }
            };
        }
        public override Guid ApplicationTypeId
        {
            get { return new Guid("{24F30C7F-7DF1-4FA2-8A36-C94EE7F5680A}"); }
        }

        public override string Name
        {
            get { return "Hosted"; }
        }

        public override string Description
        {
            get { return "Allows the CMS application to run without a custom application, e.g Webforms or MVC and allows complete development using the CMS administration interface"; }
        }

        public override ApplicationTypeConfiguration Configuration
        {
            get
            {
                return null;
            }
        }

        public override string GetPreviewUrl(Page page)
        {
            return GetPreviewUrlForPage(page);
        }

        private static string GetPreviewUrlForPage(Page page)
        {
            string url = "";
            //
            //  ok, this is our TML extension for retreiving the URL
            Page root = page;
            while (page != null)
            {
                if (page.Parent != null)
                {
                    if (page != root)
                        url = "/" + url;
                    url = page.Name + url;
                }
                page = page.Parent;
            }
            return url;
        }

        #region IPlugin Members

        public IEnumerable<IdeExtension> GetIdeExtensionsForPageTemplate(Content.PageTemplate template)
        {
            yield return urlRoutingIdeExtension;
        }

        public IEnumerable<IdeExtension> GetIdeExtensions()
        {
            yield return urlRoutingIdeExtension;
        }

        public IEnumerable<IParser> GetTmlExtensions()
        {
            yield return urlTmlExtension;
        }

        #endregion


        public IEnumerable<IdeExtension> GetIdeExtensionsForPage(Page page)
        {
            return new IdeExtension[] { };
        }

        private ICmsDataProvider provider;
        public void SetProvider(ICmsDataProvider provider)
        {
            this.provider = provider;
        }
    }
}
